﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;


public partial class admin_UploadImage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["NumricHinh"] = int.Parse(Session["NumricHinh"].ToString()) + 1;
        }
    }
    protected string FilePhoto_Upload()
    {
        string strFileName = "";
        // kiem tra xem user co chon hinh dai dien ko
        if (PhotoFile.HasFile)
        {
            // kiem tra phan mo rong cua file co hop le hay khong
            string strExtension = System.IO.Path.GetExtension(PhotoFile.FileName).ToUpper();
            if (strExtension == ".JPG" || strExtension == ".GIF" || strExtension == ".JPEG" || strExtension == ".PNG")
            {
                //System.Drawing.Image image = (System.Drawing.Image)PhotoFile;
                // lay ten file de khong bi trung file + phan mo rong cua file can upload
                string filename = PhotoFile.FileName;
                strFileName = filename.Substring(0, filename.Length - strExtension.Length)+ "_" + Session["NumricHinh"];
                if (Request["ActUpload"] == "sanpham")
                {
                    PhotoFile.PostedFile.SaveAs(Server.MapPath("~/images/Product/") + strFileName + ".jpg");
                }
                else if (Request["ActUpload"] == "tintuc")
                {
                    PhotoFile.PostedFile.SaveAs(Server.MapPath("~/images/TinTuc/") + strFileName + ".jpg");
                }
                else
                { 
                    PhotoFile.PostedFile.SaveAs(Server.MapPath("~/images/TuVan/") + strFileName + ".jpg"); 
                }
            }
            else
            {
                lblMsg.Text = "File bạn chọn upload không hợp lệ.";
                strFileName = "";
            }
        }

        return strFileName;
    }
    protected void Upload_Click(object sender, EventArgs e)
    {
        // thuc hien upload file len Server
        string FileName = FilePhoto_Upload();
        if (FileName != "")
        {
            this.Close(FileName);
        }
    }
    public void Close(string strPicture)
    {
        
        Response.Write(" <script>");
        if (Request["ActUpload"] == "sanpham")
            Response.Write(" window.opener.document.aspnetForm.imgSanPham.src ='../images/Product/" + strPicture + ".jpg'");
        else if (Request["ActUpload"] == "tintuc")
            Response.Write(" window.opener.document.aspnetForm.imgSanPham.src ='../images/TinTuc/" + strPicture + ".jpg'");
        else
            Response.Write(" window.opener.document.aspnetForm.imgSanPham.src ='../images/TuVan/" + strPicture + ".jpg'");
        Response.Write(" \n");
         Response.Write(" window.opener.document.aspnetForm.ctl00$ContentPlaceHolder1$hdimage.value ='" + strPicture + "'");
        Response.Write(" \n");
        Response.Write(" window.close();");
        Response.Write(" </script>");
    }
    public static System.Drawing.Image ScaleByPercent(System.Drawing.Image imgPhoto, int Percent)
        {
            float nPercent = ((float)Percent / 100);
 
            int sourceWidth = imgPhoto.Width;
            int sourceHeight = imgPhoto.Height;
            int sourceX = 0;
            int sourceY = 0;
 
            int destX = 0;
            int destY = 0;
            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);
 
            Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
 
            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
 
            grPhoto.DrawImage(imgPhoto,
                new Rectangle(destX, destY, destWidth, destHeight),
                new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
                GraphicsUnit.Pixel);
 
            grPhoto.Dispose();
            return bmPhoto;
        }
}